﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ODP.Interface;
using System.Diagnostics;
using ODP.Model;
using ODP.Enum;
using ODP.Com;
using System.Net.Json;

namespace ODP.QQAPI
{
    public class QQAPI : IAPI
    {
        #region 获取请求资源的url字符串
        /// <summary>
        /// 获取请求资源的url字符串
        /// </summary>
        /// <param name="tt"></param>
        /// <param name="oauth"></param>
        /// <param name="requestBaseUrl"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public APIInfo GetUrl(ref string requestUrlNoParas, ref string queryString, Enum.TwitterType tt, OAuthBase oauth,int framelevel , params Model.QueryStringParameter[] paras)
        {
            StringBuilder result = new StringBuilder();

            List<QueryStringParameter> qspList = new List<QueryStringParameter>();

            if (paras != null && paras.Length > 0)
            {
                qspList = paras.ToList();
            }

            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(System.Threading.Thread.CurrentThread, false);
            StackFrame sf = st.GetFrame(framelevel);
            string functionName = sf.GetMethod().Name;

            //获取当前方法对应的url路径以及httpmethod
            APIInfo apiInfo = oauth.apiInfoList.Where(p => p.APIFunctionName == functionName).FirstOrDefault(); ;

            if (apiInfo != null)
            {
                requestUrlNoParas = oauth.baseurl + apiInfo.APIURLPath;


                oauth.GetRequestUrl(ref queryString, requestUrlNoParas, apiInfo.APIRequestMethod.ToString(), oauth.customKey, oauth.customSecret, oauth.tokenKey, oauth.tokenSecret, "", "", qspList);
      
            }

            return apiInfo;
        }

        public APIInfo GetUrl(ref string requestUrlNoParas, ref string queryString, Enum.TwitterType tt, OAuthBase oauth, params Model.QueryStringParameter[] paras)
        {
            return GetUrl(ref requestUrlNoParas, ref queryString, tt, oauth, 2, paras);
        }
        #endregion

        #region BaseRequest
        /// <summary>
        /// BaseRequest
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string BaseRequest(OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth,tt,3,paras);
        }

        public string BaseRequest(OAuthBase oauth, Enum.TwitterType tt,int framelevel, params Model.QueryStringParameter[] paras)
        {
            string result = string.Empty;
            string requestUrlNoParas = string.Empty;
            string requestString = string.Empty;

            APIInfo apiInfo = GetUrl(ref requestUrlNoParas, ref requestString, tt, oauth,framelevel, paras);

            if (apiInfo.APIRequestMethod == HttpMethod.POST)
            {
                result = new SyncHttp().HttpPost(requestUrlNoParas, requestString);
            }
            else
            {
                result = new SyncHttp().HttpGet(requestUrlNoParas, requestString);
            }

            return result;
        }
        #endregion

        #region IAPI 获取首页信息

        public string GetHomePage(OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region IAPI 获取微博用户信息

        public string GetOtherUserInfo(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetOtherUserInfo(OAuthBase oauth, TwitterType tt,string jsonItemName, params QueryStringParameter[] paras)
        {

            string result = string.Empty;

            string userInfo = BaseRequest(oauth,tt,paras);

            if (!string.IsNullOrEmpty(userInfo))
            {
                JsonTextParser jtp = new JsonTextParser();
                JsonObjectCollection jsonUserInfo = jtp.Parse(userInfo) as JsonObjectCollection;

                if (jsonUserInfo["data"] != null)
                {
                    result = Convert.ToString((jsonUserInfo["data"] as JsonObjectCollection)[jsonItemName].GetValue());
                }
            }

            return result;
        }

        #endregion

        #region IAPI 转播
        /// <summary>
        /// 转播
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string Forward(OAuthBase oauth, Enum.TwitterType tt, params Model.QueryStringParameter[] paras)
        {
            return BaseRequest(oauth,tt,paras);
        }
        #endregion

        #region IAPI 发表评论
        /// <summary>
        /// 发表评论
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string Mention(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region IAPI 发表新微博

        /// <summary>
        /// 发表新微博
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string WeiBoAdd(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region IAPI 发表新的微博(带图片)
        /// <summary>
        /// 发表新的微博(带图片)
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string WeiBoAddWithPic(OAuthBase oauth, TwitterType tt,List<QueryStringParameter> files, params QueryStringParameter[] paras)
        {
            string result = string.Empty;
            string requestUrlNoParas = string.Empty;
            string requestString = string.Empty;

            APIInfo apiInfo = GetUrl(ref requestUrlNoParas, ref requestString, tt, oauth, 1, paras);

            result = new SyncHttp().HttpPostWithFile(requestUrlNoParas, requestString, files, paras);

            return result;
        }

        #endregion

        #region IAPI 获取当前登陆用户信息
        /// <summary>
        /// 获取当前登陆用户信息
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string GetCurrentUserInfo(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region 收听某个用户
        /// <summary>
        /// 收听某个用户
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string AddFriends(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        #endregion

        #region 特别收听某个用户
        /// <summary>
        /// 特别收听某个用户
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string AddSpecialFriends(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        #endregion

        #region 其他帐户听众列表
        /// <summary>
        /// 其他帐户听众列表
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string GetOtherUserFans(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        #endregion

        #region 其他帐户特别收听的人列表
        /// <summary>
        /// 其他帐户特别收听的人列表
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string GetOtherUserSpecialFans(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        #endregion

        #region 其他帐户收听的人列表
        /// <summary>
        /// 其他帐户收听的人列表
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string GetOtherUserIdol(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        #endregion

        #region 登陆帐户听众列表
        /// <summary>
        /// 登陆帐户听众列表
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string GetCurrentUserFans(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        #endregion

        #region 登陆帐户收听的人列表
        /// <summary>
        /// 登陆帐户收听的人列表
        /// </summary>
        /// <param name="oauth"></param>
        /// <param name="tt"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public string GetCurrentUserIdol(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }
        #endregion

        #region IAPI 邀请好友


        public string InviteFriends(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region IAPI 获取可能认识的人


        public string GetMayKownPerson(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region IAPI 获取名人列表


        public string GetFamousList(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region IAPI 成员


        public string GetHotPerson(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string GetMayKownPersonByContent(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion

        #region IAPI 成员


        public string Reply(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string RePost(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        public string AddComment(OAuthBase oauth, TwitterType tt, params QueryStringParameter[] paras)
        {
            return BaseRequest(oauth, tt, paras);
        }

        #endregion
    }
}
